package com.epfresh.adapter;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.api.web.IFileUploadChooser;
import com.epfresh.bean.DeliveryCenterListEntity;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/1/30
 * Desc:
 */
public class DeliveryCenterItemViewBinder extends ItemViewBinder<DeliveryCenterListEntity.StoreListBean, DeliveryCenterItemViewBinder.ViewHolder> {
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_merchant, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final DeliveryCenterListEntity.StoreListBean item) {
        if (item.getMarketName() != null) {
            holder.rl_title.setVisibility(View.VISIBLE);
            holder.img_recommend.setVisibility(View.GONE);
            holder.txt_more.setVisibility(View.GONE);
            holder.img_more.setVisibility(View.GONE);

            holder.txt_title.setText(item.getMarketName());

        } else {
            holder.rl_title.setVisibility(View.GONE);

        }

        /**
         * 会员 1
         * 申请中 2
         * 无 3
         * 申请会员 5
         * 重新申请 4
         */
        if (item.getStatus() == 1) {
            holder.txt_status.setVisibility(View.GONE);
//            holder.img_status.setVisibility(View.VISIBLE);
//            holder.img_status.setImageResource(R.mipmap.icon_new_vip);
            holder.img_status_vip.setVisibility(View.VISIBLE);
            holder.img_status_vip.setImageResource(R.mipmap.icon_new_vip);

        } else if (item.getStatus() == 2) {
            holder.txt_status.setVisibility(View.VISIBLE);
            holder.txt_status.setText("申请中");
            holder.txt_status.setTextColor(Color.parseColor("#898989"));
//            holder.img_status.setVisibility(View.GONE);
            holder.img_status_vip.setVisibility(View.GONE);

        } else if (item.getStatus() == 3) {
            holder.txt_status.setVisibility(View.GONE);
//            holder.img_status.setVisibility(View.GONE);
            holder.img_status_vip.setVisibility(View.GONE);

        }else if (item.getStatus() == 4){
            holder.txt_status.setVisibility(View.VISIBLE);
            holder.txt_status.setText("重新申请");
            holder.txt_status.setTextColor(Color.parseColor("#ff7900"));
//            holder.img_status.setVisibility(View.GONE);
            holder.img_status_vip.setVisibility(View.GONE);

        } else if (item.getStatus() == 5) {
            holder.txt_status.setVisibility(View.VISIBLE);
            holder.txt_status.setText("申请会员");
            holder.txt_status.setTextColor(Color.parseColor("#24af75"));
//            holder.img_status.setVisibility(View.GONE);
            holder.img_status_vip.setVisibility(View.GONE);

        }

        if (item.isEnd()) {
            holder.line_space.setVisibility(View.VISIBLE);
        } else {
            holder.line_space.setVisibility(View.GONE);
        }

        ImageLoader.getInstance().displayImage(item.getLogoUrl(), holder.img_logo, options);
        holder.txt_merchant_name.setText(item.getStoreName());
        if (item.getOrderCount() > 9999) {
            holder.txt_order_count.setText("订单量9999+");
        } else {
            holder.txt_order_count.setText("订单量" + item.getOrderCount());
        }
        if (item.getProductCount() > 9999) {
            holder.txt_goods_count.setText("商品9999+");
        } else {
            holder.txt_goods_count.setText("商品" + item.getProductCount());
        }
        holder.txt_rating.setText("好评" + item.getGoodCommentsRate() + "%");
        holder.txt_wholesale_market.setText(item.getMerchantName());



        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    if (canClick(item)){
                        onItemClickListener.onItemClick(item);
                    }
                }
            }
        });
        holder.txt_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    if (item.getStatus() != 2){
                        onItemClickListener.onApplyVIP(item);
                    }
                }
            }
        });
    }

    private boolean canClick(DeliveryCenterListEntity.StoreListBean item) {
        boolean b1 = item.getIsPublicOffer() != null && item.getIsPublicOffer().equals("1");
        boolean b2 = item.getIsPublicOffer() != null && item.getIsPublicOffer().equals("0") && item.getCustomerStatus() != null && item.getCustomerStatus().equals("2");
        return b1 || b2;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private View rl_title, line_space, ll_item;
        private ImageView img_recommend, img_more, img_logo, img_status,img_status_vip;
        private TextView txt_title, txt_more, txt_merchant_name, txt_order_count, txt_goods_count, txt_rating, txt_wholesale_market, txt_status;

        ViewHolder(View itemView) {
            super(itemView);

            ll_item = itemView.findViewById(R.id.ll_item);
            rl_title = itemView.findViewById(R.id.rl_title);
            line_space = itemView.findViewById(R.id.line_space);
            img_recommend = itemView.findViewById(R.id.img_recommend);
            img_more = itemView.findViewById(R.id.img_more);
            img_logo = itemView.findViewById(R.id.img_logo);
            img_status = itemView.findViewById(R.id.img_status);
            img_status_vip = itemView.findViewById(R.id.img_status_vip);
            txt_title = itemView.findViewById(R.id.txt_title);
            txt_more = itemView.findViewById(R.id.txt_more);
            txt_merchant_name = itemView.findViewById(R.id.txt_merchant_name);
            txt_order_count = itemView.findViewById(R.id.txt_order_count);
            txt_goods_count = itemView.findViewById(R.id.txt_goods_count);
            txt_rating = itemView.findViewById(R.id.txt_rating);
            txt_wholesale_market = itemView.findViewById(R.id.txt_wholesale_market);
            txt_status = itemView.findViewById(R.id.txt_status);
        }
    }

    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_fail_merchant) //设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.icon_fail_merchant)//设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.icon_fail_merchant)
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    public interface OnItemClickListener {
        void onItemClick(DeliveryCenterListEntity.StoreListBean item);

        void onApplyVIP(DeliveryCenterListEntity.StoreListBean item);
    }


}
